-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add nullability to more MVC assemblies #31338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| /// The delegate to invoke for creating <see cref="IStringLocalizer"/>. | ||
| /// </summary> | ||
| public Func<Type, IStringLocalizerFactory, IStringLocalizer> DataAnnotationLocalizerProvider; | ||
| public Func<Type, IStringLocalizerFactory, IStringLocalizer> DataAnnotationLocalizerProvider = null!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, weird that this is a field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow, did not notice that. API-review fail :)
| } | ||
|
|
||
| return _viewData; | ||
| return _viewData!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
??=?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess. Although the if block has a nice comment which says it's only ever run inside a unit test. I'd prefer to keep it that way.
| } | ||
|
|
||
| if (!(exception is JsonException || exception is OverflowException || exception is FormatException)) | ||
| if (exception is not null && exception is not JsonException or OverflowException or FormatException) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fancy
| } | ||
|
|
||
| public string Current => _attributes.Get(_index).Key; | ||
| public string? Current => _attributes.Get(_index).Value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Straight up bug. It's an enumerator for values (which can be null) but was returning the keys (which are non-nullable) instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing was affected by this though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently not. You'd think we have a test for this. The usually pattern is to iterate over the dictionary itself which has the correct semantics.
* [x] MVC.Cors * [x] Mvc.DataAnnotations * [x] Mvc.Localization * [x] Mvc.Newtonsoft.Json * [ ] Mvc.ViewFeatures (partial)
20f1478 to
b1ff989
Compare
b1ff989 to
76b7f9d
Compare
|
This has been updated. I also covered nullability for Mvc.Razor while addressing some feedback |
BrennanConroy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments
src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonPatchInputFormatter.cs
Outdated
Show resolved
Hide resolved
| /// An <see cref="HttpContext"/> representing the current request execution. | ||
| /// </summary> | ||
| public HttpContext Context => ViewContext?.HttpContext; | ||
| public HttpContext Context => ViewContext?.HttpContext!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ViewContext can't be null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In practice no. But in theory, if you're unit testing, you could have chosen to not initialize it. We want to avoid producing a null ref from the framework code in that case.
var page = new RazorPage();
page.HttpContext // fine if it's null here, but we want to avoid a NullReferenceException| } | ||
|
|
||
| public string Current => _attributes.Get(_index).Key; | ||
| public string? Current => _attributes.Get(_index).Value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing was affected by this though?
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| [MemberNotNull(nameof(_initialKeys), nameof(_retainedKeys), nameof(_data))] | ||
| private void AssertLoaded() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this method debug only?
| public void Reset() | ||
| { | ||
| _enumerator.Reset(); | ||
| ((IEnumerator < KeyValuePair<string, object?>>)_enumerator).Reset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, didn't get fixed
| /// </exception> | ||
| /// <returns>This <see cref="ViewEngineResult"/> if <see cref="Success"/> is <c>true</c>.</returns> | ||
| public ViewEngineResult EnsureSuccessful(IEnumerable<string> originalLocations) | ||
| [MemberNotNull(nameof(View))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't make sense
src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonPatchInputFormatter.cs
Outdated
Show resolved
Hide resolved
| /// An <see cref="HttpContext"/> representing the current request execution. | ||
| /// </summary> | ||
| public HttpContext Context => ViewContext?.HttpContext; | ||
| public HttpContext Context => ViewContext?.HttpContext!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In practice no. But in theory, if you're unit testing, you could have chosen to not initialize it. We want to avoid producing a null ref from the framework code in that case.
var page = new RazorPage();
page.HttpContext // fine if it's null here, but we want to avoid a NullReferenceException| /// Gets the dynamic view data dictionary. | ||
| /// </summary> | ||
| public dynamic ViewBag => ViewContext?.ViewBag; | ||
| public dynamic ViewBag => ViewContext?.ViewBag!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same weird deal as RazorPageBase
| { | ||
| string resolvedUrl; | ||
| if (TryResolveUrl(stringValue, resolvedUrl: out resolvedUrl)) | ||
| if (TryResolveUrl(stringValue, resolvedUrl: out string? resolvedUrl)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an overloaded method and you have to specify the type for resolution
| } | ||
|
|
||
| public string Current => _attributes.Get(_index).Key; | ||
| public string? Current => _attributes.Get(_index).Value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently not. You'd think we have a test for this. The usually pattern is to iterate over the dictionary itself which has the correct semantics.
|
Hello @pranavkm! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
Uh oh!
There was an error while loading. Please reload this page.